﻿/*
 * @author: wizardc
 */

using System;
using UnityEngine;

namespace Dou.Extend
{
    /// <summary>
    /// GameObject 方法扩展
    /// </summary>
    public static class GameObjectExtend
    {
        public static Component GetOrAddComponent(this GameObject go, Type componentType)
        {
            var component = go.GetComponent(componentType);
            if (component == null)
            {
                component = go.AddComponent(componentType);
            }
            return component;
        }
        
        public static T GetOrAddComponent<T>(this GameObject go) where T : Component
        {
            var component = go.GetComponent<T>();
            if (component == null)
            {
                component = go.AddComponent<T>();
            }
            return component;
        }
        
        public static void SetPosition(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.position = new Vector3(x, y, z);
        }

        public static void GetPosition(this GameObject gameObject, out float x, out float y, out float z)
        {
            var position = gameObject.transform.position;
            x = position.x;
            y = position.y;
            z = position.z;
        }

        public static void SetLocalPosition(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.localPosition = new Vector3(x, y, z);
        }
        
        public static void GetLocalPosition(this GameObject gameObject, out float x, out float y, out float z)
        {
            var localPosition = gameObject.transform.localPosition;
            x = localPosition.x;
            y = localPosition.y;
            z = localPosition.z;
        }
        
        public static void SetRotation(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.rotation = Quaternion.Euler(x, y, z);
        }

        public static void GetRotation(this GameObject gameObject, out float x, out float y, out float z)
        {
            var eulerAngles = gameObject.transform.rotation.eulerAngles;
            x = eulerAngles.x;
            y = eulerAngles.y;
            z = eulerAngles.z;
        }

        public static void SetLocalRotation(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.localRotation = Quaternion.Euler(x, y, z);
        }

        public static void GetLocalRotation(this GameObject gameObject, out float x, out float y, out float z)
        {
            var localEulerAngles = gameObject.transform.localRotation.eulerAngles;
            x = localEulerAngles.x;
            y = localEulerAngles.y;
            z = localEulerAngles.z;
        }

        public static void SetForward(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.forward = new Vector3(x, y, z);
        }

        public static void RotationWorld(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.Rotate(x, y, z, Space.World);
        }

        public static void RotationSelf(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.Rotate(x, y, z, Space.Self);
        }
        
        public static void SetLocalScale(this GameObject gameObject, float x, float y, float z)
        {
            gameObject.transform.localScale = new Vector3(x, y, z);
        }
        
        public static void GetLocalScale(this GameObject gameObject, out float x, out float y, out float z)
        {
            var localScale = gameObject.transform.localScale;
            x = localScale.x;
            y = localScale.y;
            z = localScale.z;
        }
        
        public static void SetLayer(this GameObject gameObject, string name)
        {
            var layer = LayerMask.NameToLayer(name);
            gameObject.layer = layer;
        }
        
        public static void DestroySelf(this GameObject go, float delayTime = 0.0f)
        {
            GameObject.Destroy(go, delayTime);
        }
    }
}
